package net.chenyan.server.controller.admapi;/**
 * @author chenyan
 * @createTime 2022/5/8 21:54
 * @description description
 */

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.chenyan.commons.core.R;
import net.chenyan.server.dto.ListDTO;
import net.chenyan.server.po.*;
import net.chenyan.server.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 *@description: TODO
 *@author: chenyan
 *@create: 2022/5/8 21:54
 */
@Slf4j
@Api(tags = {"用户创建音乐歌单接口"})
@RestController
@RequestMapping(value = "/admapi/userList")
public class UserListController {

    @Autowired
    private ListService listService;
    @Autowired
    private MusicService musicService;
    @Autowired
    private UserService userService;
    @Autowired
    private ListMusicService listMusicService;
    @Autowired
    private UserListService userListService;

    @ApiOperation(value = "用户创建歌单")
    @PostMapping(value = "/addList")
    public R addList(@Validated @RequestBody ListDTO listDTO) {
        QueryWrapper<List> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("create_user_id",listDTO.getCreateUserId());
        queryWrapper.eq("name",listDTO.getName());
        List list=listService.getOne(queryWrapper);
        if(list!=null){
            return R.ok("该歌单已存在，请在歌单中添加歌曲！");
        }
        List list1=new List();
        list1.setName(listDTO.getName());
        list1.setCreateUserId(listDTO.getCreateUserId());
        list1.setPicture(listDTO.getPicture());
        boolean temp = listService.save(list1);
        return temp ? R.ok("创建成功!") : R.error("创建失败！");
    }

    @ApiOperation(value = "用户往歌单中添加音乐")
    @PostMapping(value = "/addMusic")
    public R addMusic(@RequestBody int listId ,int musicId) {
        List list=listService.getById(listId);
        Music music=musicService.getById(musicId);
        if(list==null||music==null){
            return R.error("歌单不存在/歌曲不存在");
        }
        QueryWrapper<ListMusic> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("music_id",musicId);
        queryWrapper.eq("list_id",listId);
        ListMusic listMusic=listMusicService.getOne(queryWrapper);
        if(listMusic!=null){
            return R.error("歌曲已经在歌单中啦！");
        }
        ListMusic listMusic1=new ListMusic();
        listMusic1.setListId(listId);
        listMusic1.setMusicId(musicId);
        boolean temp = listMusicService.save(listMusic1);
        return temp ? R.ok("添加歌曲成功!") : R.error("添加歌曲失败！");
    }


    @ApiOperation(value = "获取该用户音乐歌单列表")
    @PostMapping(value = "/getUserList")
    public R getUserList(@RequestParam(defaultValue = "0")int page, @RequestParam(defaultValue = "5")int size, int userId) {
        return R.data(listService.findAllByCreateUserId(PageRequest.of(page,size),userId));
    }

    @ApiOperation(value = "用户点赞/取消点赞歌单")
    @PostMapping(value = "/likeList")
    public R likeList(@RequestBody int userId, int listId) {
        User user=userService.getById(userId);
        List list=listService.getById(listId);
        if(user==null || list==null){
            return R.error("用户id或者歌单id不存在！");
        }
        int likeNum=list.getLikedNum();
        QueryWrapper<UserList> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("list_id",listId);
        UserList userList=userListService.getOne(queryWrapper);
        if(userList==null){
            UserList userList1=new UserList();
            userList1.setListId(listId);
            userList1.setUserId(userId);
            userList1.setIsLike(1);
            userListService.save(userList1);
            list.setLikedNum(likeNum+1);
            listService.updateById(list);
            return R.ok("点赞成功");
        }else{
            if(userList.getIsLike()==1){
                userList.setIsLike(0);
                userListService.updateById(userList);
                list.setLikedNum(likeNum-1);
                listService.updateById(list);
                return R.ok("取消点赞成功");
            }else{
                userList.setIsLike(1);
                userListService.updateById(userList);
                list.setLikedNum(likeNum+1);
                listService.updateById(list);
                return R.ok("点赞成功");
            }
        }
    }

    @ApiOperation(value = "用户收藏/取消收藏歌单")
    @PostMapping(value = "/collectList")
    public R collectList(@RequestBody int userId,int listId) {
        User user=userService.getById(userId);
        List list=listService.getById(listId);
        if(user==null || list==null){
            return R.error("用户id或者歌单id不存在！");
        }
        int collectNum=list.getCollectedNum();
        QueryWrapper<UserList> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("list_id",listId);
        UserList userList=userListService.getOne(queryWrapper);
        if(userList==null){
            UserList userList1=new UserList();
            userList1.setListId(listId);
            userList1.setUserId(userId);
            userList1.setIsCollect(1);
            userListService.save(userList1);
            list.setCollectedNum(collectNum+1);
            listService.updateById(list);
            return R.ok("收藏成功");
        }else{
            if(userList.getIsCollect()==1){
                userList.setIsCollect(0);
                userListService.updateById(userList);
                list.setCollectedNum(collectNum-1);
                listService.updateById(list);
                return R.ok("取消收藏成功");
            }else{
                userList.setIsCollect(1);
                userListService.updateById(userList);
                list.setCollectedNum(collectNum+1);
                listService.updateById(list);
                return R.ok("收藏成功");
            }
        }
    }

    @ApiOperation(value = "用户评论歌单")
    @PostMapping(value = "/commentList")
    public R commentList(@RequestBody int userId,int listId,String comment) {
        User user=userService.getById(userId);
        List list=listService.getById(listId);
        if(user==null || list==null){
            return R.error("用户id或者歌单id不存在！");
        }
        QueryWrapper<UserList> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("list_id",listId);
        UserList userList=userListService.getOne(queryWrapper);
        if(userList==null){
            UserList userList1=new UserList();
            userList1.setListId(listId);
            userList1.setUserId(userId);
            userList1.setComment(comment);
            userListService.save(userList1);
            return R.ok("评论成功");
        }else{
            userList.setComment(comment);
            boolean temp = userListService.updateById(userList);
            return temp ? R.ok("修改成功") : R.error("修改失败");
        }
    }

    @ApiOperation(value = "获取用户收藏的歌单")
    @PostMapping(value = "/getListByUserId")
    public R getListByUserId(int userId,@RequestParam(defaultValue = "0")int page, @RequestParam(defaultValue = "5")int size) {
        return R.data(listService.findAllListByUserId(PageRequest.of(page,size),userId));
    }



}
